﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dtos;
using Core.Entities;
using Core.ViewModels;

namespace Assembler
{
    public static class AutoMapperConfig
    {
        private static bool s_isInit = false;

        public static void CreateMappings()
        {
            if (s_isInit)
                return;
            s_isInit = true;

            AutoMapper.Mapper.CreateMap<User, UserDto>();
            AutoMapper.Mapper.CreateMap<UserDto, User>()
                      .ForMember(x => x.PasswordHash, opt => opt.Ignore())
                      .ForMember(x => x.SecurityStamp, opt => opt.Ignore())
                      .ForMember(x => x.Discriminator, opt => opt.Ignore())
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());                      

            AutoMapper.Mapper.CreateMap<Article, ArticleListVM>();
            AutoMapper.Mapper.CreateMap<Article, ArticleDto>();
            AutoMapper.Mapper.CreateMap<ArticleDto, Article>()
                      .ForMember(x => x.ID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Columns, opt => opt.Ignore())
                      .ForMember(x => x.UserID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.User, opt => opt.Ignore())
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<Column, ColumnDto>();
            AutoMapper.Mapper.CreateMap<ColumnDto, Column>()
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())                      
                      .ForMember(x => x.Article, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<Item, ItemDto>();
            AutoMapper.Mapper.CreateMap<Item, SimpleListExportItemVM>();
            AutoMapper.Mapper.CreateMap<ItemDto, Item>()
                      .ForMember(x => x.ID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.DataItems, opt => opt.Ignore())
                      .ForMember(x => x.ArticleID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Article, opt => opt.Ignore())
                      .ForMember(x => x.DateCreated, opt => opt.Ignore())
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<ItemValue, ItemValueDto>();
            AutoMapper.Mapper.CreateMap<ItemValueDto, ItemValue>()
                      .ForMember(x => x.ID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Column, opt => opt.Ignore())
                      .ForMember(x => x.ColumnID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Item, opt => opt.Ignore())                      
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<MotorPolicy, MotorPolicyDto>();
            AutoMapper.Mapper.CreateMap<MotorPolicyDto, MotorPolicy>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Person, opt => opt.Ignore())
                      .ForMember(x => x.PersonID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<Person, PersonDto>();
            AutoMapper.Mapper.CreateMap<PersonDto, Person>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.Policies, opt => opt.Ignore());

            AutoMapper.Mapper.CreateMap<Policy, PolicyDto>()
                      .Include<MotorPolicy, MotorPolicyDto>();
            AutoMapper.Mapper.CreateMap<PolicyDto, Policy>()
                      .Include<MotorPolicyDto, MotorPolicy>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.Person, opt => opt.Ignore())
                      .ForMember(x => x.PersonID, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore());

            AutoMapper.Mapper.AssertConfigurationIsValid();
        }
    }
}      